package thread2;

/**
 * Created with IntelliJ IDEA.
 * Description:wait和notify案例
 * User: bjc
 * Date: 2025-08-20
 * Time: 21:15
 */
public class ThreadDemo9 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(()->{
            synchronized (locker){
                System.out.println("t1线程wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1线程wait之后");
            }
        });
        Thread t2 = new Thread(()->{
            //为了保证线程t1先获取到锁，所以先让线程t2休眠
            try {
                Thread.sleep(5000);
                synchronized (locker){
                    System.out.println("t2线程notify之前");
                    locker.notify();
                    System.out.println("t2线程notify之后");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t2.start();
    }
}
